package com.ght.springboot.elasticsearch;

import com.google.gson.GsonBuilder;
import io.searchbox.client.JestClient;
import io.searchbox.client.JestClientFactory;
import io.searchbox.client.config.HttpClientConfig;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.Objects;

/**
 * @description: Elasticsearch 配置
 * @author: haitao.gao
 * @create: 2019-02-25 09:25
 **/
@Component
public class ElasticsearchDsCfg {


//    @Value("${elasticsearch.order.host}")
    private String test="http://101.132.36.129";
//    private static final String ES_HOST = "http://devdb.zbycorp.com";

//    @Value("${elasticsearch.order.port}")
    private String ES_HTTP_PORT="9200";

//    @Value("${elasticsearch.order.connect-timeout}")
    private String ES_CONNECT_TIMEOUT="10000";

//    @Value("${elasticsearch.order.default-connect}")
    private String ES_DEFAULT_CONNECT="2";

//    @Value("${elasticsearch.order.max-connect}")
    private Integer ES_MAX_CONNECT=2;

//    @Value("${elasticsearch.order.read-timeout}")
    private String ES_READ_TIMEOUT="10000";

//    @Value("${elasticsearch.order.password}")
    private String ES_PASSWORD = "null";

//    @Value("${elasticsearch.order.username}")
    private String ES_USERNAME = "null";

    private static JestClient client;

    public static ElasticsearchDsCfg elasticsearchDsCfg;

    @PostConstruct
    public void init() {
        elasticsearchDsCfg = this;
    }


    /**
     * 获取客户端
     *
     * @return jestclient
     */
    public synchronized JestClient getClient() {
        if (client == null) {
            build();
        }
        return client;
    }

    /**
     * 关闭客户端
     */
    public void close(JestClient client) {
        if (!Objects.isNull(client)) {
            try {
                client.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 建立连接
     */
    private void build() {
        JestClientFactory factory = new JestClientFactory();
        factory.setHttpClientConfig(
                new HttpClientConfig
                        .Builder(test + ":" + ES_HTTP_PORT)
                        .multiThreaded(true)
                        //一个route 默认不超过2个连接  路由是指连接到某个远程注解的个数。总连接数=route个数 * defaultMaxTotalConnectionPerRoute
                        .defaultMaxTotalConnectionPerRoute(Integer.valueOf(ES_DEFAULT_CONNECT))
                        //所有route连接总数
                        .maxTotalConnection(Integer.valueOf(ES_MAX_CONNECT))
                        .connTimeout(Integer.valueOf(ES_CONNECT_TIMEOUT))
                        .readTimeout(Integer.valueOf(ES_READ_TIMEOUT))
                        .gson(new GsonBuilder()
                                .setDateFormat("yyyy-MM-dd HH:mm:ss")
                                .create())
                        .defaultCredentials(StringUtils.isNotBlank(ES_USERNAME)?ES_USERNAME:"null",
                                            StringUtils.isNotBlank(ES_PASSWORD)?ES_PASSWORD:"null")
                        .build()
        );
        client = factory.getObject();
    }
}
